% optimize the network based on sparseness measure
function optimize_map

clear lowest_cost

global Ne IN N A AI Achand best

% call simulator once to define the area sizes as globals.
opt_fun = 'spnet_som_local_min';
net_fun = 'spnet_som_test';

eval(net_fun);

% Parallel Toolbox usage.
if matlabpool('size') == 0
%    matlabpool open 8
end


max_conn_data(1,:) = [30  200 length(A)/2 ];
max_conn_data(2,:) = [30  200 length(AI)/2 ];
max_conn_data(3,:) = [30  200 length(AI)/2 ];
max_conn_data(4,:) = [30  200 length(Achand)/2 ];
max_conn_data(5,:) = [30  200 length(A)/2 ];
max_conn_data(6,:) = [30  200 length(A)/2 ];
max_conn_data(7,:) = [30  400 length(A)/2 ];
max_conn_data(8,:) = [30  400 length(A)/2 ];

min_conn_data(1,:) = [.5   .001 1 ];
min_conn_data(2,:) = [.5   .001 1 ];
min_conn_data(3,:) = [.5   .001 1 ];
min_conn_data(4,:) = [.5   .001 1 ];
min_conn_data(5,:) = [.5   .001 1 ];
min_conn_data(6,:) = [.5   .001 1 ];
min_conn_data(7,:) = [.5   .001 1 ];
min_conn_data(8,:) = [.5   .001 1 ];

min_conn_data
max_conn_data

i=1;


radius=length(A)/4;
conn_data(i,:)=[0.5*radius  20 radius ];i=i+1;

radius=length(AI)/4;
conn_data(i,:)=[0.5*radius  20 radius ];i=i+1;

radius=length(AI)/4;
conn_data(i,:)=[0.5*radius  20 radius ];i=i+1;

radius=length(Achand)/4;
conn_data(i,:)=[radius  2 radius ];i=i+1;

radius = length(A)/6;
conn_data(i,:)=[0.5*radius   150 radius ];i=i+1;

radius = length(A)/2;
conn_data(i,:)=[0.5*radius  150 radius ];i=i+1;

radius = length(A)/2;
conn_data(i,:)=[0.5*radius  150 radius ];i=i+1;

radius = length(AI)/2;
conn_data(i,:)=[0.5*radius  150 radius ];i=i+1;

min_group_data(1,:)=[0 0 0 0 1 1 .1];
min_group_data(2,:)=[0 0 0 0 1 1 .1];
min_group_data(3,:)=[0 0 0 0 1 1 .1];
min_group_data(4,:)=[0 0 0 0 1 1 .1];

max_group_data(1,:)=[30 30 1 1 2000 2000 1];
max_group_data(2,:)=[30 30 1 1 2000 2000 1];
max_group_data(3,:)=[30 30 1 1 2000 2000 1];
max_group_data(4,:)=[30 30 1 1 2000 2000 1];

% +background -background nmdagain gababgain  f d u
group_data(1,:)=    [1 1 .5 0 5 300 .5];
group_data(2,:)=    [1 1 .5 0 1000 800 .5];
group_data(3,:)=    [1 1 .5 0 1000 800 .5];
group_data(4,:)=    [1 1 .1 0 20   700 .2];


min_conn_data=[min_conn_data(:) ; min_group_data(:)];
max_conn_data=[max_conn_data(:) ; max_group_data(:)];
conn_data=[conn_data(:) ; group_data(:)];


%connect(AI,A,'gauss', 'std', conn(1), 'mininitw', conn(2), 'maxinitw',
%conn(3), 'radius', conn(4), 'delaymin', conn(5), 'delaymax', conn(6), 'lrate', conn(7), 'minw', conn(8), 'maxw', conn(9));

lowest_cost = 1e6;
for i = 1:1
    
    conn_data
    
    % evolutionary search strategy combined with local min search.
    opts.LBounds = min_conn_data(:); opts.UBounds = max_conn_data(:); 
    %opts.Noise.on=1; 
    sigma = abs(max_conn_data(:)-min_conn_data(:))*.5;
    [best_params, cost_measure] = cmaes( opt_fun, conn_data(:), sigma, opts, min_conn_data, max_conn_data);

    
    
    %[best_params, cost_measure] = fmincon( @spnet_som, conn_data, [], [] , [],[],min_conn_data, max_conn_data);

    % evolutionary search strategy.
    %opts.LBounds = min_conn_data(:); opts.UBounds = max_conn_data(:); 
    %sigma = 5;
    %[best_params, cost_measure] = cmaes( 'local_min_spnet_som', conn_data(:), sigma, opts, min_conn_data, max_conn_data);


    if cost_measure < lowest_cost
        lowest_cost = cost_measure
        save 'best' best_params cost_measure;
    end
    
    
    [cost sp]=spnet_som(best_params);
    plot(sp(:,1),sp(:,2),'.');

end



function new_conn = tweak(conn_data)

global Ne IN N A AI



new_conn(1,:) = conn_data(1,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(2,:) = conn_data(2,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(3,:) = conn_data(3,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(4,:) = conn_data(4,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(5,:) = conn_data(5,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(6,:) = conn_data(6,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];

% max wt should be >= min wt.
bad_indices = conn_data(:,9) < new_conn(:,8);
new_conn( bad_indices, 8 ) = new_conn(bad_indices, 9); 

bad_indices = conn_data(:,6) < new_conn(:,5);
new_conn( bad_indices, 5 ) = new_conn(bad_indices, 6); 


max_conn_data(1,:) = [30 5 20 length(A)/2 5 5 .1 5 20];
max_conn_data(2,:) = [30 5 20 length(AI)/2 5 5 .1 5 20];
max_conn_data(3,:) = [30 5 20 length(AI)/2 5 5 .1 5 20];
max_conn_data(4,:) = [30 5 20 length(A)/2 5 5 .1 5 20];
max_conn_data(5,:) = [30 5 20 length(A)/2 5 5 .1 5 20];
max_conn_data(6,:) = [30 0 0 length(A)/2 5 5 .1 5  0];

min_conn_data(1,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(2,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(3,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(4,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(5,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(6,:) = [.5 -15 -20 1 1 1 0 -20 0];

new_conn = max(min_conn_data,min(max_conn_data,new_conn));
%connect(IN,A,'gauss', 'std', conn(1), 'mininitw', conn(2), 'maxinitw', conn(3), 'radius', conn(4), 'delaymin', conn(5), 'delaymax', conn(6), 'lrate', conn(7), 'minw', conn(8), 'maxw', conn(9));



